<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/28
 * Time: 上午11:50
 * Describe: synchronization order status between tmall1 and d_try.order_info by order_log
 */

namespace console\models;

use Yii;
use yii\console\Exception;

/**
 * @property \yii\db\Connection db
 */
class SyncOrderStatus extends Object
{


    public $db;

    public function init()
    {
        $this->db = Yii::$app->getDb();
        parent::init();
    }


    public function run()
    {
        $this->toTmall1OrderLog();
        $this->updateAllStatus();
    }

    /**
     * 导入订单日志从后端数据库到前端数据库
     * @throws \yii\db\Exception
     */
    public function toTmall1OrderLog()
    {
        try {
            $last_time = Yii::$app->redis->get("order_log_last_id");
            if (empty($id)) {
                static::info("export d_try.order_log to tmall1 failed:can't found last order_log_id");
                return;
            }

            $result = $this->db->createCommand("INSERT INTO `tmall1`.`order_log`
                (`start_status`, `addtime`, `start_pay_status`, `order_id`, `signed_time`, `end_status`, `end_pay_status`, `admin_uid`)
                  SELECT
                    `start_status`,
                    unix_timestamp(`addtime`),
                    `start_pay_status`,
                    `order_info_id`,
                    `signed_time`,
                    `end_status`,
                    `end_pay_status`,
                    `admin_uid`
                  FROM `d_try`.`order_log`
                  WHERE order_log_id > {$id} ")
                               ->execute();
            static::info('export d_try.order_log to tmall1 success:' . $result);

        } catch (Exception $e) {
            static::info('export d_try.order_log to tmall1 failed:' . $e->getMessage());
        }
    }


    /**
     * 同步订单状态
     * @throws \yii\db\Exception
     */
    public function updateAllStatus()
    {
        $transaction = $this->db->beginTransaction();
        try {
            $result = $this->db->createCommand("
                                UPDATE
                                    tmall1.order_info t, d_try.order_info o
                                SET t.status = o.status
                                WHERE t.uid = o.order_info_id
                                      AND o.status > t.status
                                      AND (o.order_info_id > 40000000 OR o.order_info_id < 20000000)
                                      AND t.addtime > (unix_timestamp(now()) - 90 * 24 * 3600);

                                -- 更新订单状态

                                UPDATE
                                    d_try.order_info o, tmall1.order_info t
                                SET o.status = t.status
                                WHERE t.uid = o.order_info_id
                                      AND t.status > o.status
                                      AND (o.order_info_id > 40000000 OR o.order_info_id < 20000000)
                                      AND t.addtime > (unix_timestamp(now()) - 90 * 24 * 3600);

                                -- 更新支付状态

                                UPDATE
                                    tmall1.order_info t, d_try.order_info o
                                SET
                                  t.pay_result = o.pay_status
                                WHERE t.uid = o.order_info_id
                                      AND o.pay_status <> t.pay_result AND t.pay_result = 3
                                      AND (o.order_info_id > 40000000 OR o.order_info_id < 20000000)
                                      AND t.addtime > (unix_timestamp(now()) - 90 * 24 * 3600);

                                -- 更新支付状态

                                UPDATE
                                    tmall1.order_info t, d_try.order_info o
                                SET
                                  o.pay_status = t.pay_result
                                WHERE t.uid = o.order_info_id
                                      AND o.pay_status <> t.pay_result AND o.pay_status = 3
                                      AND (o.order_info_id > 40000000 OR o.order_info_id < 20000000)
                                      AND t.addtime > (unix_timestamp(now()) - 90 * 24 * 3600);

                                -- 更新快递信息

                                UPDATE d_try.order_express e, tmall1.order_info t
                                SET e.delivery_code = t.delivery_code,
                                  e.delivery_id     = t.delivery_id,
                                  e.delivery_time   = t.delivery_time
                                WHERE e.order_info_id = t.uid
                                      AND t.delivery_code <> ''
                                      AND t.delivery_code != e.delivery_code;")
                               ->execute();
            $transaction->commit();
            static::info('sync order status success:' . $result . ' rows');
        } catch (Exception $e) {
            $transaction->rollBack();
            static::info('sync order status:' . $e->getMessage());
        }
    }


}


